package hlhj.fhp.supreme.activitys
import android.annotation.SuppressLint
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.Gravity
import android.view.WindowManager
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.ToastUtils
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.MainActivity
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.CityPop
import hlhj.fhp.supreme.customview.RegisTypePop
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.PhoneBean
import hlhj.fhp.supreme.javabean.RegisBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import hlhj.fhp.supreme.utils.SPHelper
import kotlinx.android.synthetic.main.activity_regis_student_aty.*
import java.util.ArrayList

class RegisStudentAty : BaseAty(), CityPop.OnResult, RegisTypePop.OnRegisTypeClickListener {

    private var cid=""
    private var pid=""
    private var aid=""
    private var sec=60
    private var etStr="1"
    private val datas=ArrayList<String>()
    private lateinit var typePop:RegisTypePop
    private val  handler= @SuppressLint("HandlerLeak")
    object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            if (sec>0){
                sec--
                btSms.text="$sec s"
                this.sendEmptyMessageDelayed(1,1000)
            }else{
                sec=60
                btSms.text="验证码"
                btSms.isClickable=true
            }
        }
    }
    override fun getContentId(): Int {
        return R.layout.activity_regis_student_aty
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    }
    override fun onDestroy() {
        super.onDestroy()
        handler.removeMessages(1)
    }

    override fun initView() {
        getType()
        etStr = intent.getStringExtra("et")
        if (etStr.equals("2")){
            tvTittle.text="绑定手机"
        }
        OkGo.post<PhoneBean>(Urls.BaseUrl+Urls.PHONE)
                .params("type","type")
                .execute(object :JsonCallBack<PhoneBean>(this){
                    override fun onSuccess(response: Response<PhoneBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            typePop= RegisTypePop(this@RegisStudentAty,bean?.data.type as ArrayList<String>,this@RegisStudentAty)
                        }
                    }
                })
    }

    private fun getType() {
        OkGo.post<PhoneBean>(Urls.BaseUrl+Urls.PHONE)
                .params("type","type")
                .execute(object :JsonCallBack<PhoneBean>(this){
                    override fun onSuccess(response: Response<PhoneBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                        }
                    }
                })
    }

    override fun initListener() {

        btRule.setOnClickListener {
            val intent = Intent(this, RuleAty::class.java)
            startActivity(intent)
        }

        btExit.setOnClickListener { finish() }
        btChoose.setOnClickListener {

            val cityPop = CityPop(this, this)
            cityPop.showAtLocation(father,Gravity.BOTTOM,0,0)
        }

        btSms.setOnClickListener {
            LogUtil.log("点击了！！")
            if (etPhone.text.toString().length!=11){
                LogUtil.log("点击了！！不正确")
                MyUtils.toast("手机号码格式不正确！")
            }else{
                LogUtil.log("点击了！！正确")
                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.SEND_SMS)
                        .params("mobile",etPhone.text.toString())
                        .execute(object :JsonCallBack<BaseBean>(){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                if (bean?.code==1){
                                    MyUtils.toast("发送成功")
                                    btSms.text="$sec s"
                                    btSms.isClickable=false
                                    handler.sendEmptyMessage(1)
                                }
                            }
                        })
            }
        }

        btCommit.setOnClickListener {
            if (btCheck.isChecked){

                if (etPhone.text.toString().length==0||etName.text.toString().length==0||
                        etPwd.text.toString().length==0||etYzm.text.toString().length==0||
                        etCity.text.toString().length==0||etHobby.text.toString().length==0
                        ){
                    if (etPhone.text.isEmpty()){
                        MyUtils.toast("手机号未填写")
                        return@setOnClickListener
                    }
                    if (etName.text.toString().isEmpty()){
                        MyUtils.toast("姓名未填写")
                        return@setOnClickListener
                    }
                    if (etPwd.toString().isEmpty()){
                        MyUtils.toast("密码未填写")
                        return@setOnClickListener
                    }

                    if (etYzm.toString().isEmpty()){
                        MyUtils.toast("验证码未填写")
                        return@setOnClickListener
                    }
                    if (etCity.text.isEmpty()){
                        MyUtils.toast("所在地未填写")
                        return@setOnClickListener
                    }
                    if (etHobby.text.isEmpty()){
                        MyUtils.toast("偏好未填写")
                        return@setOnClickListener
                    }
                }else{
                    when(etStr){
                        "1"->{
                            OkGo.post<RegisBean>(Urls.BaseUrl+Urls.REGIS)
                                    .params("mobile",etPhone.text.toString())
                                    .params("name",etName.text.toString())
                                    .params("user_pass",etPwd.text.toString())
                                    .params("captcha",etYzm.text.toString())
                                    .params("province_id",cid)
                                    .params("city_id",pid)
                                    .params("area_id",aid)
                                    .params("friend_code",etYqm.text.toString())
                                    .params("like_type",etHobby.text.toString())
                                    .execute(object :JsonCallBack<RegisBean>(){
                                        override fun onSuccess(response: Response<RegisBean>?) {
                                            val bean = response?.body()
                                            if (bean?.code==1){
                                                SPHelper.saveToken(this@RegisStudentAty,bean.data.token)
                                                finish()
                                                MyUtils.toast("注册成功")
                                            }else{
                                                MyUtils.toast(bean?.msg)
                                            }
                                        }
                                    })
                        }
                        "2"->{
                            OkGo.post<RegisBean>(Urls.BaseUrl+Urls.REGIS)
                                    .params("mobile",etPhone.text.toString())
                                    .params("name",etName.text.toString())
                                    .params("user_pass",etPwd.text.toString())
                                    .params("captcha",etYzm.toString())
                                    .params("province_id",cid)
                                    .params("city_id",pid)
                                    .params("area_id",aid)
                                    .params("like_type",etHobby.text.toString())
                                    .params("openid",intent.getStringExtra("openid"))
                                    .params("friend_code",etYqm.text.toString())
                                    .params("type",intent.getStringExtra("type"))
                                    .execute(object :JsonCallBack<RegisBean>(){
                                        override fun onSuccess(response: Response<RegisBean>?) {
                                            val bean = response?.body()
                                            if (bean?.code==1){
                                                SPHelper.saveToken(this@RegisStudentAty,bean.data.token)
                                                startActivity(Intent(this@RegisStudentAty,MainActivity::class.java))
                                                finish()
                                                MyUtils.toast("注册成功")
                                            }else{
                                                MyUtils.toast(bean?.msg)
                                            }
                                        }
                                    })
                        }
                    }

                }
            }else{
                MyUtils.toast("你没有同意用户协议")
            }
        }

        btType.setOnClickListener {
            if (typePop.isShowing){
//                ToastUtils.toast(this,"可多选")
                typePop.dismiss()
            }else{
                ToastUtils.toast(this,"偏好种类可以多选")
                typePop.showAsDropDown(btType)
            }
        }
    }
    override fun onResult(cTv: String, pTv: String, aTv: String, cid: String, pid: String, aId: String) {
        etCity.setText(cTv+""+pTv+""+aTv)
        this.cid=cid
        this.pid=pid
        this.aid=aId
    }

    /**
     * 类型点击
     */
    override fun onClick(name: String) {
        if (etHobby.text.isEmpty()){
            etHobby.setText(etHobby.text.toString()+name)
        }else{
            etHobby.setText(etHobby.text.toString()+"-"+name)
        }
    }



}
